#   Copyright 2006 Jerome Lacoste (jerome@coffeebreaks.org)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
class LoginController < ApplicationController

  before_filter :authorize, :except => [:login, :help, :add_user]
  before_filter :authorize_add_user, :only => [:add_user,]

  def index
    @total_clients = Client.count
    @total_projects = Project.count
  end

  def add_user
    if request.get?
      @user = User.new
    else
      @user = User.new(params[:user])
      # might be nice to auto-detect the timezone somehow
#      @user.tz = tzinfo_from_timezone(TimeZone.new(params[:user][:timezone_name])
      if @user.save
        redirect_to_index("User, #{@user.name} created")
      end
    end
  end

  def profile
    @user = User.find(session[:user_id])
    if !request.get?
#      tz = TimeZone.new(params[:user][:timezone_name])
#      @user.tz = tzinfo_from_timezone(tz)
      # update_attributes doesn't handle aggregation
      @user.timezone_name = params[:user][:timezone_name]
      if @user.update_attributes(params[:user])
        flash[:notice] = 'Profile successfully updated'
      end
    end
  end

  def tzinfo_from_timezone(timezone) 
#    return nil if timezone.nil?
    TZInfo::Timezone.all.each do |tz|
#      logger.info(tz)
#      logger.info(timezone)
#      logger.info(tz.current_period)
      if tz.current_period.utc_offset.to_i == timezone.utc_offset.to_i
        return tz
      end
    end
    return nil   
  end

  def login
    if request.get?
      session[:user_id] = nil
      @user = User.new
    else
      @user = User.new(params[:user])
      logged_in_user = @user.try_to_login
      if logged_in_user
        session[:user_id] = logged_in_user.id
        jumpto = session[:jumpto] || {:action => "help"}
        session[:jumpto] = nil
        redirect_to(jumpto)
      else
        flash[:notice] = "Invalid user/password combination"
      end
    end
  end

  def logout
    session[:user_id] = nil
    flash[:notice] = "logged out"
    redirect_to(:action => "login")
  end

  def delete_user
    id = params[:id]
    if id && user = User.find(id)
      begin
        user.destroy
        flash[:notice] = "User #{user.name} deleted"
      rescue
        flash[:notice] = "Can't delete that user"
      end
    end
    redirect_to :action => "list_users"
  end

  def list_users
    @sorter = SortingHelper::Sorter.new self, %w(name short), @params['sort'], @params['order'], 'name', 'ASC'
    @user_pages, @users = paginate :users, :per_page => 10, :order => @sorter.to_sql
  end
end
